package com.cw2.structural.flyweight;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;

public class ZuDao {

    public static Map<String, AbstractWaitressFlyweight> pool = new HashMap<>();

    static {
        BeautifulWaitress waitress1 = new BeautifulWaitress("11111", "张三", 20);
        BeautifulWaitress waitress2 = new BeautifulWaitress("22222", "李四", 24);
        addWaitress(waitress1);
        addWaitress(waitress2);
    }

    public static void addWaitress(AbstractWaitressFlyweight waitressFlyweight) {
        pool.put(UUID.randomUUID().toString(), waitressFlyweight);
    }

    public static AbstractWaitressFlyweight getWaitress(String name) {
        AbstractWaitressFlyweight waitressFlyweight = pool.get(name);
        if (Objects.nonNull(waitressFlyweight)) {
            return waitressFlyweight;
        } else {
            for (AbstractWaitressFlyweight value : pool.values()) {
                if (value.isCanService()) {
                    return value;
                }
            }
        }
        return null;
    }
}
